package com.hq.ims.data.service;

import com.hq.ims.data.dao.PermissionMapAuthorityUserMapper;
import com.hq.ims.data.entity.Company;
import com.hq.ims.data.entity.PermissionMapAuthorityUser;
import com.hq.ims.data.entity.User;
import com.hq.ims.data.utils.ErrMsgConstant;
import com.hq.ims.data.vo.PermissionMapAuthorityUserMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.BooleanUtils;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.ObjectUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 * @Detail 组-用户关联表服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class BasePermissionMapAuthorityUserService extends BaseService<PermissionMapAuthorityUserMapper, PermissionMapAuthorityUser> {

    @Resource
    private BasePermissionAuthorityService basePermissionAuthorityService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BasePermissionMapAuthorityRoleService permissionMapAuthorityRoleService;
    @Resource
    private BasePermissionMapAuthorityMenuService basePermissionMapRoleMenuService;
    @Resource
    private BasePermissionMenuService basePermissionMenuService;
    @Resource
    private BasePermissionRoleService basePermissionRoleService;
    @Resource
    private BaseCompanyDepartmentEmployeeService baseCompanyMapDepartmentUserService;


    /**
     * 根据权限ID和公司ID删除权限(删除指定权限,指定用户,指定公司)
     *
     * @param authId 组ID
     */
    public boolean delByAuthIdAndUserIdAndCompanyId(
            @NonNull String authId,
            @NonNull String userId,
            @NonNull String companyId
    ) {
        return this.lambdaUpdate()
                .eq(PermissionMapAuthorityUser::getPermissionAuthorityId, authId)
                .eq(PermissionMapAuthorityUser::getUserId, userId)
                .eq(PermissionMapAuthorityUser::getCompanyId, companyId)
                .remove();
    }

    /**
     * 根据权限ID删除(用于权限删除后所有拥有这个权限的用户也没有了权限)
     *
     * @param authId 组ID
     */
    public boolean delByAuthId(
            @NonNull String authId
    ) {
        return this.lambdaUpdate()
                .eq(PermissionMapAuthorityUser::getPermissionAuthorityId, authId)
                .remove();
    }

    /**
     * 删除
     *
     * @param userId 用户ID
     */
    public Boolean delByUserIdAndCompanyId(
            @NonNull String userId,
            @NonNull String companyId
    ) {
        return this.lambdaUpdate()
                .eq(PermissionMapAuthorityUser::getUserId, userId)
                .eq(PermissionMapAuthorityUser::getCompanyId, companyId)
                .remove();
    }


    /**
     * 绑定用户角色
     */
    @Transactional(rollbackFor = Exception.class)
    public void bindUserAuth(
            PermissionMapAuthorityUserMapstruct.BindUserAuthVo saveVo
    ) {
        //1.清除之前的数据 根据用户的公司id
        this.delByUserIdAndCompanyId(saveVo.getUserId(), saveVo.getCompanyId());
        //2: 添加用户&角色绑定关系
        User user = baseUserService.getById(saveVo.getUserId());
        ObjectUtils.isNullThrow(user, ErrMsgConstant.USER_INFO_NOT_EXISTS);

        Company company = baseCompanyService.getById(saveVo.getCompanyId());
        ObjectUtils.isNullThrow(company, ErrMsgConstant.USER_INFO_NOT_EXISTS);

        saveVo.getPermissionAuthorityIdList().forEach(authId -> {

            boolean isExistsCompanyAuth = basePermissionAuthorityService.isExistsCompanyAuth(authId, saveVo.getCompanyId());
            BooleanUtils.isFalseThrow(isExistsCompanyAuth, ErrMsgConstant.PERMISSION_AUTH_NOT_EXISTS);

            PermissionMapAuthorityUser permissionMapGroupUser = new PermissionMapAuthorityUser();
            permissionMapGroupUser.setPermissionAuthorityId(authId);
            permissionMapGroupUser.setUserId(saveVo.getUserId());
            permissionMapGroupUser.setCompanyId(saveVo.getUserId());
            permissionMapGroupUser.insert();
        });
    }

    /**
     * 根据用户id获取所有角色id(实际为组id)
     *
     * @param userId 用户ID
     */
    public List<String> getAuthIdListByUserIdAndCompanyId(
            @NonNull String userId,
            @NonNull String companyId
    ) {
        List<PermissionMapAuthorityUser> permissionMapGroupUsers = this.lambdaQuery()
                .eq(PermissionMapAuthorityUser::getUserId, userId)
                .eq(PermissionMapAuthorityUser::getCompanyId, companyId)
                .list();
        if (ListUtils.isNotExistsEle(permissionMapGroupUsers)) {
            return ListUtils.newEmptyList();
        }
        return permissionMapGroupUsers.stream().map(PermissionMapAuthorityUser::getCompanyId).distinct().collect(Collectors.toList());
    }
}
